///////////////////////////////////////////////////////////////////////////////
// Project: Hoyo Client Game
//
// hy_network_message.h
// Created: 2012-10-26
// Author: Like Cheng
///////////////////////////////////////////////////////////////////////////////
#pragma once
#include "hy_includes.h"
#include "hy_file_system/hy_stream.h"

#ifndef __HY_NETWORK_MESSAGE_H_
#define __HY_NETWORK_MESSAGE_H_

class hy_network_message : public HY_NS::hy_stream
{
public:
	typedef boost::shared_ptr< hy_network_message > ptr;
	hy_network_message();
	hy_network_message( int msg_type );
	hy_network_message( const char* str, int len );
	virtual ~hy_network_message();

	static hy_network_message::ptr create( const char* str, int len );
	
public: // stream
	bool					hy_write_end();
	bool					hy_write_schar( schar c, int pos = 0 );
	bool					hy_write_uchar( uchar c, int pos = 0  );
	bool					hy_write_uint16( uint16 c, int pos = 0  );
	bool					hy_write_sint16( sint16 c, int pos = 0  );
	bool					hy_write_uint32( uint32 c, int pos = 0  );
	bool					hy_write_sint( sint c, int pos = 0  );
	bool					hy_write_float32( float32 c, int pos = 0  );
	bool					hy_write_uint64( uint64 c, int pos = 0  );
	bool					hy_write_string( const char *c, int pos = 0  );

	int						get_new_pos( int pos );

public: //Json value	
	void					write_int( int key, int msg );
	void					write_int( const char* key , int msg );

	void					write_double( int key, double msg );
	void					write_double( const char* key, double msg );

	void					write_string( int index, const char* msg );
	void					write_string( const char* key, const char* msg );

	void					write_value( const char* key, Json::Value v );

	int						read_int( int key );
	int						read_int( const char* key );

	double					read_double( int key );
	double					read_double( const char* key );

	std::string				read_string( int key );
	std::string				read_string( const char* key );
	Json::Value				read_value( const char* key );

	Json::Value				&get_value(){ return m_value; }
	void					set_value( Json::Value &value ){ m_value = value; }

	void					set_player_id( int player_id ){ m_player_id = player_id; }
	void					set_net_id( int net_id ){ m_net_id = net_id; }
	void					set_protocol_id( int protocol_id ){ m_protocol_id = protocol_id; }

	int						get_player_id(){ return m_player_id; }
	int						get_net_id(){ return m_net_id; }
	int						get_protocol_id(){ return m_protocol_id; }
	int						get_total_length();

	void					set_msg_type( char msg_type ){ m_msg_type = msg_type; }
	char					get_msg_type(){ return m_msg_type; }

	void					clear_string();
	std::string				get_string();
	std::string				get_head_string();
	std::string				get_content_string();
	std::string&			get_value_string(){ return m_value_str; }
	bool					send();


	std::string				dencrypt_msg(  int count, const char* key, const char * src_data );

	string_map*				get_string_map(){ return m_string_map; }
private:
	Json::Value				m_value;
	std::string				m_value_str;
	int						m_player_id;
	int						m_net_id;
	int						m_protocol_id;
	char					m_msg_type;
	string_map*				m_string_map;
};

class hy_message_logic
{
public:
	hy_message_logic();
	virtual ~hy_message_logic();
	typedef std::vector< hy_network_message* >	hy_network_message_vec;

	static hy_message_logic	*	instance();

	static std::string			assembly_message( uint player_id, uint net_id, uint protocol_id, hy_network_message &msg );

	bool					process_message( const char* c, int len );

private:
	hy_network_message_vec				m_buffer;
	static hy_message_logic	*			instance_p_;
};

#endif